package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.pojo.Dept;
import com.qk.pojo.Role;
import com.qk.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/roles")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 新增角色
     * @param role
     * @return
     */
    @PostMapping
    public Result addRole(@RequestBody Role role) {
        log.info("新增角色: {}" , role);
        roleService.add(role);
        return Result.success() ;
    }

    /**
     * 分页查询角色
     * @param name
     * @param label
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public Result listRoles(String name,String label,
                           @RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("条件分页查询角色: name={}, label={}, page={}, pageSize={}", name, label, page, pageSize);
        PageResult<Role> pageResult = roleService.listRoles(name,label,page,pageSize);
        return Result.success(pageResult);
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteRole(@PathVariable("id") Integer id) {

        log.info("删除角色: {}", id);
        roleService.deleteRole(id);
        return Result.success();
    }

    /**
     * 修改角色
     * @param role
     * @return
     */
    @PutMapping
    public Result updateRole(@RequestBody Role role) {
        log.info("修改角色: {}" , role);
        roleService.updateRole(role);
        return Result.success();
    }

    /**
     * 根据ID查询角色
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getRoleById(@PathVariable Integer id) {
        log.info("根据ID查询角色: {}" , id);
        Role role = roleService.getRoleById(id);
        return Result.success(role);
    }

    /**
     * 查询所有角色
     * @return
     */
    @GetMapping("/lists")
    public Result findAllRoles() {
        log.info("查询所有角色");
        List<Role> roleList = roleService.list();
        return Result.success(roleList);
    }




}
